import request from '@/utils/request'
import method from '@/utils/config'

const prefix = '/crm/customer'

// 请求路径
const customerApi = {
  // 分页查询客户数据
  listByParamsForTable: prefix + '/listByParamsForTable',
  // 插入一条客户，返回主键
  saveHasKey: prefix + '/saveHasKey',
  // 根据id批量删除客户
  deleteBatchByIds: prefix + '/deleteBatchByIds',
  // 根据主键有选择性更新客户
  updateByPrimaryKeySelective: prefix + '/updateByPrimaryKeySelective',
  // 根据id查询客户
  selectByPrimaryKey: prefix + '/selectByPrimaryKey',
  // 修改客户成交状态
  updateDealStatus: prefix + '/updateDealStatus',
  // 查询客户下联系人
  listContacts: prefix + '/listContacts',
  // 查询客户下商机
  listBusiness: prefix + '/listBusiness',
  // 查询客户下合同
  listContract: prefix + '/listContract',
  // 锁定或解锁客户
  lock: prefix + '/lock',
  // 修改客户负责人
  changeOwnerUser: prefix + '/changeOwnerUser',
  // 查询客户下回款
  listReceivables: prefix + '/listReceivables',
  // 查询客户下回款计划
  listReceivablesPlan: prefix + '/listReceivablesPlan',
  // 查询客户下发票信息
  listInvoiceInfo: prefix + '/listInvoiceInfo',
  // 查询客户下发票
  listInvoice: prefix + '/listInvoice'
}

/**
 * 按照条件分页查询
 * @param queryParams 查询对象
 * @returns {AxiosPromise}
 */
export function listByParamsForTable(queryParams) {
  return request({
    url: customerApi.listByParamsForTable,
    method: method.POST,
    data: {
      limit: queryParams.limit,
      page: queryParams.page,
      search: queryParams.search
    }
  })
}

/**
 * 插入一条数据,返回主键
 * @param data 要插入的数据
 * @returns {*}
 */
export function saveHasKey(data) {
  return request({
    url: customerApi.saveHasKey,
    method: method.POST,
    data
  })
}

/**
 * 根据id批量删除
 * @param ids id集合
 * @returns {*}
 */
export function deleteBatchByIds(ids) {
  return request({
    url: customerApi.deleteBatchByIds + `/${ids}`,
    method: method.DELETE
  })
}

/**
 * 根据主键有选择性更新记录
 * @param data 要更新的记录
 * @returns {*}
 */
export function updateByPrimaryKeySelective(data) {
  return request({
    url: customerApi.updateByPrimaryKeySelective,
    method: method.PUT,
    data
  })
}

/**
 * 根据id查询记录
 * @param id 查询id
 * @returns {*}
 */
export function selectByPrimaryKey(id) {
  return request({
    url: customerApi.selectByPrimaryKey,
    method: method.GET,
    params: {
      id
    }
  })
}

/**
 * 更改成交状态
 * @param id id
 * @param dealStatus 状态
 * @returns {*}
 */
export function updateDealStatus(id, dealStatus) {
  return request({
    url: customerApi.updateDealStatus,
    method: method.PUT,
    params: {
      id,
      dealStatus
    }
  })
}

/**
 * 查询客户下联系人
 * @param queryParams 查询对象
 * @returns {AxiosPromise}
 */
export function listContactsFromCustomer(queryParams) {
  return request({
    url: customerApi.listContacts,
    method: method.POST,
    data: {
      page: queryParams.page,
      limit: queryParams.limit,
      search: queryParams.search,
      searchId: queryParams.searchId
    }
  })
}

/**
 * 查询客户下商机
 * @param queryParams 查询对象
 * @returns {AxiosPromise}
 */
export function listBusinessFromCustomer(queryParams) {
  return request({
    url: customerApi.listBusiness,
    method: method.POST,
    data: {
      page: queryParams.page,
      limit: queryParams.limit,
      search: queryParams.search,
      searchId: queryParams.searchId
    }
  })
}

/**
 * 查询客户下合同
 * @param queryParams 查询对象
 * @returns {AxiosPromise}
 */
export function listContractFromCustomer(queryParams) {
  return request({
    url: customerApi.listContract,
    method: method.POST,
    data: {
      page: queryParams.page,
      limit: queryParams.limit,
      search: queryParams.search,
      searchId: queryParams.searchId
    }
  })
}

/**
 * 锁定或解锁客户
 * @param status 状态
 * @param customerId 客户id
 * @returns {*}
 */
export function lock(status, customerId) {
  return request({
    url: customerApi.lock,
    method: method.put,
    params: {
      status,
      customerId
    }
  })
}

/**
 * 查询客户下回款
 * @param queryParams 查询对象
 * @returns {AxiosPromise}
 */
export function listReceivablesFromCustomer(queryParams) {
  return request({
    url: customerApi.listReceivables,
    method: method.POST,
    data: {
      page: queryParams.page,
      limit: queryParams.limit,
      search: queryParams.search,
      searchId: queryParams.searchId
    }
  })
}

/**
 * 查询客户下回款计划
 * @param queryParams 查询对象
 * @returns {AxiosPromise}
 */
export function listReceivablesPlanFromCustomer(queryParams) {
  return request({
    url: customerApi.listReceivablesPlan,
    method: method.POST,
    data: {
      page: queryParams.page,
      limit: queryParams.limit,
      search: queryParams.search,
      searchId: queryParams.searchId
    }
  })
}

/**
 * 查询客户下发票信息
 * @param queryParams 查询对象
 * @returns {AxiosPromise}
 */
export function listInvoiceInfoFromCustomer(queryParams) {
  return request({
    url: customerApi.listInvoiceInfo,
    method: method.POST,
    data: {
      page: queryParams.page,
      limit: queryParams.limit,
      search: queryParams.search,
      searchId: queryParams.searchId
    }
  })
}

/**
 * 查询客户下发票
 * @param queryParams 查询对象
 * @returns {AxiosPromise}
 */
export function listInvoiceFromCustomer(queryParams) {
  return request({
    url: customerApi.listInvoice,
    method: method.POST,
    data: {
      page: queryParams.page,
      limit: queryParams.limit,
      search: queryParams.search,
      searchId: queryParams.searchId
    }
  })
}

/**
 * 修改客户负责人
 * @param data 负责人修改对象
 * @returns {AxiosPromise}
 */
export function changeOwnerUser(data) {
  return request({
    url: customerApi.changeOwnerUser,
    method: method.PUT,
    data: {
      id: data.id,
      ownerId: data.ownerId,
      ownerUserName: data.ownerUserName,
      changeType: data.changeType,
      transferType: data.transferType,
      power: data.power
    }
  })
}

